﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using SpringWaterDataAccess;
using System.Text;
using System.Web.Mail;

namespace client
{
	/// <summary>
	/// Summary description for WebForm5.
	/// </summary>
	public partial class WebForm5 : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.Button Submit1;
		protected System.Web.UI.HtmlControls.HtmlForm frmRegister;
	
		protected void Page_Load(object sender, System.EventArgs e)
		{
			// Put user code to initialize the page here
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    

		}
		#endregion

        //protected void aaRegister_Click(object sender, System.EventArgs e)
        //{
        //    if (Page.IsValid)
        //    {
        //        string password = idPassword.Value;
        //        string nickname = idNickName.Value;

        //        //default password is the charcters before @
        //        /**if (password == "") 
        //        {
        //            password = password.Substring(0,password.IndexOf("@") + 1);
        //        }

        //        if (nickname == "") 
        //        {
        //            nickname = password;
        //        }**/

        //        string faith = RadioButtonList1.SelectedValue ;

        //       // all validation passed, now call db to register user, if anything goes wrong, re-direct error page
        //        UserObject uo= UserObject.Register(idEmail.Value, 
        //            password,nickname, "",faith);

        //        if (null != uo)
        //        {
        //            HttpCookie userNickNameCookie = new HttpCookie("NickName", nickname);
        //            Response.Cookies.Add(userNickNameCookie);
        //            HttpCookie userEmailCookie = new HttpCookie("Email", idEmail.Value);
        //            Response.Cookies.Add(userEmailCookie);
        //            //FormsAuthentication.RedirectFromLoginPage(idEmail.Value,true);
        //            //login

        //            EmailToClient(nickname,idEmail.Value,"",password);

					
        //            Response.Redirect("index.aspx");
        //        } 
        //        else 
        //        {
        //            //database error				
        //        }
			
        //    }
        //}

		private void EmailToClient(string nickName, string email, string recommendBy, string password)
		{
			StringBuilder body = new StringBuilder();

            body.Append("亲爱的" + nickName + "：\n");

			body.Append("谢谢你经" + recommendBy);
			
			body.Append("推荐加入<泉源网>。我们的使命是要使<泉源网>成为国内读者进入海内外基督教网站和杂志，或者认识不同教会、牧师、和传道人的窗口。\n");

			body.Append("你的用户名(笔名)是: " + nickName + ", 密码是: " + password + ", 如果你需要修改登记信息,请点击:\n");

			body.Append("http://www.springofwater.net/springofwater-gospel/client/dengji.aspx \n");

			body.Append("作为<泉源网>的登记用户，你将能够：\n\n");

			body.Append("1. 阅读，收看和收听最新的基督福音信息和资料。\n");
			body.Append("2. 通过使用’我的书架’功能，收集和管理你自己喜欢的文章。每当你看到喜欢的文章，请点击‘放入我的书架‘。\n");
			body.Append("3. 参加<泉源论坛>的讨论:\n");
			body.Append("http://www.springofwater.net/springofwater-gospel/client/forumsStart.aspx\n");

			body.Append("请将泉源网站推荐给你的家人和朋友.\n\n");

			body.Append("谢谢\n");
			body.Append("愿上帝祝福你\n");
			body.Append("泉源网\n");
			body.Append("http://www.springofwater.net\n");		


			MailMessage objEmail = new MailMessage();
			objEmail.To       = email;
			objEmail.From     = "contact@springofwater.net";
			objEmail.Subject  = "欢迎加入泉源网";
			objEmail.Body     = body.ToString();
			objEmail.Priority = MailPriority.High;
			SmtpMail.SmtpServer = "mail.gus.net";
			
			try
			{
				SmtpMail.Send(objEmail);
			} 
			catch (Exception exc)
			{
				Response.Write("Send failure: " + exc.ToString());
			}
		}

        protected void aaRegister_Click1(object sender, EventArgs e)
        {


            if (Page.IsValid)
            {
                string password = idPassword.Value;
                string nickname = idNickName.Value;

                //default password is the charcters before @
                /**if (password == "") 
                {
                    password = password.Substring(0,password.IndexOf("@") + 1);
                }

                if (nickname == "") 
                {
                    nickname = password;
                }**/

                string faith = RadioButtonList1.SelectedValue;

                string  ipAddress = Request.UserHostAddress;

                // all validation passed, now call db to register user, if anything goes wrong, re-direct error page
                UserObject uo = UserObject.Register(idEmail.Value,
                    password, nickname, "", faith,ipAddress);

                if (null != uo)
                {
                    HttpCookie userNickNameCookie = new HttpCookie("NickName", nickname);
                    Response.Cookies.Add(userNickNameCookie);
                    HttpCookie userEmailCookie = new HttpCookie("Email", idEmail.Value);
                    Response.Cookies.Add(userEmailCookie);
                    //FormsAuthentication.RedirectFromLoginPage(idEmail.Value,true);
                    //login

                    EmailToClient(nickname, idEmail.Value, "", password);


                    Response.Redirect("index.aspx");
                }
                else
                {
                    //database error				
                }

            }
        }

	}


}
